package com.example.chenduoyun.safe.receiver;

import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.telephony.SmsManager;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.example.chenduoyun.safe.utils.ConstantValue;
import com.example.chenduoyun.safe.utils.SpUtils;

import static android.content.ContentValues.TAG;

/**
 * @author by created chen cloudy 2018/10/15 14:14
 **/

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.i(TAG, "cdy onReceive: 开关机广播");
        String spSimSerialNumber = SpUtils.getString(context,
                ConstantValue.SIM_SERIAL_NUMBER, "");

        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        String simSerialNumber = tm.getSimSerialNumber() + "xxx";
        if(!spSimSerialNumber.equals(simSerialNumber)){
            SmsManager sm =SmsManager.getDefault();
            short g=16;
            byte[] bytes = "SIM CHANGE!!!".getBytes();
            String phone=SpUtils.getString(context,ConstantValue.CONTACT_PHONE,"");
            sm.sendDataMessage(phone,null,g,
                    bytes,null,null);
        }
    }
}
